import { Injectable } from '@angular/core';
import { Platform,NavController  } from '@ionic/angular';
import { Router } from '@angular/router';
import { Toast } from '@ionic-native/toast/ngx';
import { ToastController } from '@ionic/angular';
import { CommonService,ToastColor } from './common-service.service';

@Injectable({
  providedIn: 'root'
})
export class RegisterButtonService {

  private backButtonPressed:boolean  = false;

  constructor(
    private platform: Platform,
    private router: Router,
    private commonService:CommonService,
    private toastController: ToastController,
    public navController: NavController//导航控制器
  ) { }
  
  registerBackButtonAction(){
    this.platform.backButton.subscribe(()=>{
      if(this.router.url == "/tabs/home" 
            || this.router.url == "/tabs/myCredit"
            || this.router.url == "/tabs/myAccount"){
        if (this.backButtonPressed) {
          this.backButtonPressed = false;
          navigator['app'].exitApp();
          this.toastController.dismiss();
        } else {
          this.commonService.commonToast(ToastColor.dark,'Press again to exit the program');
          this.backButtonPressed = true;
          setTimeout(() => this.backButtonPressed = false, 1000);
        }
        return false;
      }else if(this.router.url == "/auth/signin-password"
            || this.router.url == "/auth/signin-sms"
            || this.router.url == "/auth/signin-fingerprint"
            || this.router.url == "/auth/signin-first"
      ){
        navigator['app'].exitApp();
      }else{
        this.navController.back();
      }
    });
  }

}
